package com.example.core.arch.advice;

import com.alibaba.fastjson.JSONObject;
import com.example.core.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class ControllerAdvice {

    /**
     * controller环绕通知，打印请求，返回参数，请求耗时
     */
    @Around(value = "execution(* com.example.core.controller.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        if (BaseController.class.getName().equals(pjp.getSignature().getDeclaringTypeName())) {
            return pjp.proceed();
        }
        String name = pjp.getSignature().getName();
        log.info("{} is start, req：{}", name, JSONObject.toJSONString(pjp.getArgs()));
        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();
        log.info("{} is over, resp：{}, cost: {}ms", name, JSONObject.toJSONString(result), end - start);
        return result;
    }

}
